<template>
  <div class="productList">
    <ul>
        <!-- 可以采用router-link 进行导航，这里采取学习新的方式 编程导航，即JS -->
      <!-- <router-link :to="'/home/productInfo/'+ item.id" v-for="item in productList" :key="item.id" tag="li">
        <img :src="item.img_url" alt />
        <h4 class="title">{{item.title}}</h4>
        <div class="info">
          <p class="price">
            <span>￥{{item.sell_price}}</span>
            <del>￥{{item.market_price}}</del>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{item.stock_quantity}}件</span>
          </p>
        </div>
      </router-link> -->
      <li v-for="item in productList" :key="item.id" @click="getDetail(item.id)">
        <img :src="item.img_url" alt />
        <h4 class="title">{{item.title}}</h4>
        <div class="info">
          <p class="price">
            <span>￥{{item.sell_price}}</span>
            <del>￥{{item.market_price}}</del>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{item.stock_quantity}}件</span>
          </p>
        </div>
      </li>
    </ul>
    <van-button type="danger" block @click="getMoreProduct()">加载更多</van-button>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      pageIndex: 1, // 默认值为1
      productList: [],
    };
  },
  created() {
    this.getProduct();
  },
  methods: {
    getProduct() {
      this.$http
        .get(
          "http://www.liulongbin.top:3005/api/getgoods?pageindex=" +
            this.pageIndex
        )
        .then(res => {
          if (res.data.status === 0) {
              res.data.message.length ? res.data.message : Toast("没有更多数据")
            this.productList = this.productList.concat(res.data.message);
            //  console.log(this.productList);
          }
        })
        .catch(function(error) {
          // handle error
          Toast("获取商品列表失败");
        });
    },
    getMoreProduct(){
        this.pageIndex ++;
        this.getProduct()
    },
    getDetail(id){
        this.$router.push({ name: 'productInfo', params: { id: id }})
        
    }
  }
};
</script>
<style lang="less" scoped>
.productList {
  background-color: #fff;
  ul {
    width: 100%;
    li {
      float: left;
      width: 50%;
      padding: 5px;
      box-shadow: 2px 0 2px #ccc;
      h4 {
        font-size: 14px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      img {
        width: 100%;
        height: 100%;
        min-width: 177.5px;
        min-height: 177.5px;
      }
      .info {
        background-color: #eee;
        padding: 5px;
        .price {
          span {
            color: orangered;
            font-weight: bolder;
          }
        }
        .sell {
          display: flex;
          justify-content: space-between;
          margin-bottom: 0;
        }
      }
    }
  }
  van-button {
    margin: 5px 0;
  }
}
</style>